package com.yunkeji.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunkeji.common.core.domain.PageQuery;
import com.yunkeji.common.core.page.TableDataInfo;
import com.yunkeji.common.utils.StringUtils;
import com.yunkeji.dao.domain.SysVersion;
import com.yunkeji.dao.mapper.SysVersionMapper;
import com.yunkeji.dao.vo.SysVersionVo;
import com.yunkeji.system.domain.bo.SysVersionBo;
import com.yunkeji.system.service.ISysVersionService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * APP版本信息Service业务层处理
 *
 * @author zhang
 * @date 2024-07-10
 */
@RequiredArgsConstructor
@Service
public class SysVersionServiceImpl implements ISysVersionService {

    private final SysVersionMapper baseMapper;

    /** 查询APP版本信息 */
    @Override
    public SysVersionVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /** 查询APP版本信息列表 */
    @Override
    public TableDataInfo<SysVersionVo> queryPageList(SysVersionBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SysVersion> lqw = buildQueryWrapper(bo);
        Page<SysVersionVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /** 查询APP版本信息列表 */
    @Override
    public List<SysVersionVo> queryList(SysVersionBo bo) {
        LambdaQueryWrapper<SysVersion> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SysVersion> buildQueryWrapper(SysVersionBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysVersion> lqw = Wrappers.lambdaQuery();
        lqw.eq(ObjectUtil.isNotNull(bo.getValidFlag()), SysVersion::getValidFlag, bo.getValidFlag());
        lqw.eq(StringUtils.isNotEmpty(bo.getVersionCode()), SysVersion::getVersionCode, bo.getVersionCode());
        lqw.eq(ObjectUtil.isNotNull(bo.getForceUpdate()), SysVersion::getForceUpdate, bo.getForceUpdate());
        lqw.eq(ObjectUtil.isNotNull(bo.getBranch()), SysVersion::getBranch, bo.getBranch());
        lqw.orderByDesc(SysVersion::getVersionIndex);
        return lqw;
    }

    /** 新增APP版本信息 */
    @Override
    public Boolean insertByBo(SysVersionBo bo) {
        SysVersion add = BeanUtil.toBean(bo, SysVersion.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            //  bo.setId(add.getId());
        }
        return flag;
    }

    /** 修改APP版本信息 */
    @Override
    public Boolean updateByBo(SysVersionBo bo) {
        SysVersion update = BeanUtil.toBean(bo, SysVersion.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /** 保存前的数据校验 */
    private void validEntityBeforeSave(SysVersion entity) {
        // TODO 做一些数据校验,如唯一约束
    }

    /** 批量删除APP版本信息 */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            // TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
